'use strict';

const Service = require('egg').Service;

class OrderService extends Service {

  // 创建订单
  async createOrder(Order) {
    const { app } = this;
    return await app.model.MallOrder.create(Order);
  }


  // 获取列表
  async get(payload) {
    const { app } = this;
    const { pageNum, pageSize } = payload;
    const { count, rows } = await app.model.MallOrder.findAndCountAll({
      order: [[ 'createdAt', 'desc' ]],
      offset: (pageNum - 1) * pageSize,
      limit: pageSize * 1,
    });
    return {
      pageNum,
      pageSize,
      total: count,
      list: rows,
    };
  }

  // 删除
  async del(payload) {
    const { app } = this;
    return await app.model.MallOrder.destroy({ where: { id: payload.ids } });
  }

  // 修改订单状态
  async update(id, status) {
    const { app } = this;
    return await app.model.MallOrder.update({ status }, { where: { id } });
  }


}

module.exports = OrderService;
